Add tests for GdkRGBA serialization
authorMatthias Clasen <mclasen@redhat.com>
Mon, 25 Oct 2010 16:10:48 +0000 (12:10 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 25 Oct 2010 16:10:48 +0000 (12:10 -0400)
In particular, test that serialization is not dependent on the
locale.

gdk/gdkrgba.c
gdk/tests/gdk-color.c

index 68f602d71a5f75807679d935baa5b36315e6fa46..e26de9806efe09969bde6c18216ad332b9a627f3 100644 (file)
@@ -269,9 +269,15 @@ gdk_rgba_equal (gconstpointer p1,
 gchar *
 gdk_rgba_to_string (GdkRGBA *rgba)
 {
-  return g_strdup_printf ("rgba(%f,%f,%f,%f)",
-                          CLAMP (rgba->red, 0, 1),
-                          CLAMP (rgba->green, 0, 1),
-                          CLAMP (rgba->blue, 0, 1),
-                          CLAMP (rgba->alpha, 0, 1));
+  gchar red[G_ASCII_DTOSTR_BUF_SIZE];
+  gchar green[G_ASCII_DTOSTR_BUF_SIZE];
+  gchar blue[G_ASCII_DTOSTR_BUF_SIZE];
+  gchar alpha[G_ASCII_DTOSTR_BUF_SIZE];
+
+  g_ascii_dtostr (red, G_ASCII_DTOSTR_BUF_SIZE, CLAMP (rgba->red, 0, 1));
+  g_ascii_dtostr (green, G_ASCII_DTOSTR_BUF_SIZE, CLAMP (rgba->green, 0, 1));
+  g_ascii_dtostr (blue, G_ASCII_DTOSTR_BUF_SIZE, CLAMP (rgba->blue, 0, 1));
+  g_ascii_dtostr (alpha, G_ASCII_DTOSTR_BUF_SIZE, CLAMP (rgba->alpha, 0, 1));
+
+  return g_strdup_printf ("rgba(%s,%s,%s,%s)", red, green, blue, alpha);
 }
index f2346de25116167902f48b15075064c7ae6cd2ee..b0736b8b3ecff72d4c6ed0fdb38b2c1b46dcf622 100644 (file)
@@ -1,3 +1,4 @@
+#include <locale.h>
 #include <gdk/gdk.h>
 
 static void
@@ -50,6 +51,42 @@ test_color_parse (void)
   g_assert (gdk_rgba_equal (&color, &expected));
 }
 
+static void
+test_color_to_string (void)
+{
+  GdkRGBA rgba;
+  GdkRGBA out;
+  gchar *res;
+  gchar *res_de;
+  gchar *res_en;
+  gchar *orig;
+
+  rgba.red = 1.0;
+  rgba.green = 0.5;
+  rgba.blue = 0.1;
+  rgba.alpha = 1.0;
+
+  orig = g_strdup (setlocale (LC_ALL, NULL));
+  res = gdk_rgba_to_string (&rgba);
+  gdk_rgba_parse (res, &out);
+  g_assert (gdk_rgba_equal (&rgba, &out));
+
+  setlocale (LC_ALL, "de_DE.utf-8");
+  res_de = gdk_rgba_to_string (&rgba);
+  g_assert_cmpstr (res, ==, res_de);
+
+  setlocale (LC_ALL, "en_US.utf-8");
+  res_en = gdk_rgba_to_string (&rgba);
+  g_assert_cmpstr (res, ==, res_en);
+
+  g_free (res);
+  g_free (res_de);
+  g_free (res_en);
+
+  setlocale (LC_ALL, orig);
+  g_free (orig);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -57,6 +94,7 @@ main (int argc, char *argv[])
         gdk_init (&argc, &argv);
 
         g_test_add_func ("/color/parse", test_color_parse);
+        g_test_add_func ("/color/to-string", test_color_to_string);
 
         return g_test_run ();
 }